package com.example.experiment3.controller;

import com.example.experiment3.model.RegionStats;
import com.example.experiment3.service.CoronavirusDataService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/Data")
public class DataController {
    @Autowired
    private CoronavirusDataService coronavirusDataService;

    @GetMapping("/getIndex")
    public String index( Model model ) {
        List<RegionStats> allRegionsStats = coronavirusDataService.getAllRegionsStats();
        model.addAttribute("allRegionsStats",allRegionsStats);
        return "index";
    }

    @RequestMapping(value = "/findByCountry",method = RequestMethod.POST)
    public String findByCountryName(String countryname,Model model){
        List<RegionStats> res = coronavirusDataService.getAllRegionsStats().stream()
                .filter(rs -> rs.getCountry().contains(countryname))
                .collect(Collectors.toList());//抽取集
        model.addAttribute("res",res);
        return "search";
    }

    @RequestMapping(value = "/China",method = RequestMethod.GET)
    public String China(String cn,Model model){
        cn="China";
        String finalCn = cn;
        List<RegionStats> res = coronavirusDataService.getAllRegionsStats().stream()
                .filter(rs -> rs.getCountry().contains(finalCn))
                .collect(Collectors.toList());//抽取集合
        model.addAttribute("res",res);
        return "search";
    }

    @RequestMapping(value = "/data",method = RequestMethod.GET)
    @ResponseBody
    public List<RegionStats> data(String countryname, Model model){
        String cn="China";
        String finalCn = cn;
        List<RegionStats> COVIDData = coronavirusDataService.getAllRegionsStats().stream()
                .filter(rs -> rs.getCountry().contains(finalCn))
                .collect(Collectors.toList());//抽取集合
        List<RegionStats> Top10=new ArrayList<>();
//        for(int i=0;i<10;i++){
//            Top10.add(COVIDData.get(i));
//        }
        return COVIDData;
    }

    @RequestMapping(value = "/todata",method = RequestMethod.GET)
    public String todata(Model model){
        return "data";
    }


}
